/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketManager.ejb;

import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import ticketManager.model.AssignEvent;
import ticketManager.model.ChangeProblemStateEvent;
import ticketManager.model.CreateProblemEvent;
import ticketManager.model.HistoryEvent;
import ticketManager.model.LoginEvent;
import ticketManager.model.LogoutEvent;
import ticketManager.model.ManagerUser;
import ticketManager.model.Problem;

/**
 *
 * @author luchenyuxx
 */
@Stateless
public class HistoryDAO {

    @PersistenceContext
    EntityManager em;

    public String loginEvent(ManagerUser u) {

        LoginEvent l = new LoginEvent();
        l.setOperator(u);
        Date d = new Date();
        l.setDate(d);
//        HistoryEvent his = new HistoryEvent();
//        his.setDate(d);
//        his.setHistoryevent(l.toString());
//        em.persist(his);
        em.persist(l);
        return "success";
    }

    public String logoutEvent(ManagerUser u) {
        LogoutEvent l = new LogoutEvent();
        l.setOperator(u);
        Date d = new Date();
        l.setDate(d);
//        HistoryEvent his = new HistoryEvent();
//        his.setDate(d);
//        his.setHistoryevent(l.toString());
//        em.persist(his);
        em.persist(l);
        return "success";
    }

    public String createProblemEvent(ManagerUser u, Problem p) {
        CreateProblemEvent c = new CreateProblemEvent();
        c.setOperator(u);
        c.setTarget(p);
        Date d = new Date();
        c.setDate(d);
//        HistoryEvent his = new HistoryEvent();
//        his.setDate(d);
//        his.setHistoryevent(c.toString());
//        em.persist(his);
        em.persist(c);
        return "success";
    }
    public String changeProblemStateEvent(ManagerUser u, Problem p) {
        ChangeProblemStateEvent c = new ChangeProblemStateEvent();
        c.setOperator(u);
        c.setTarget(p);
        Date d = new Date();
        c.setDate(d);
//        HistoryEvent his = new HistoryEvent();
//        his.setDate(d);
//        his.setHistoryevent(c.toString());
//        em.persist(his);
        em.persist(c);
        return "success";
    }
    public String assignEvent(Long mid, Long uid, Long pid) {
        System.err.println("assignEvent is executed");
        ManagerUser m=em.find(ManagerUser.class, mid);
        ManagerUser u=em.find(ManagerUser.class, uid);
        Problem p=em.find(Problem.class, pid);
        AssignEvent a = new AssignEvent();     
        a.setOperator(m);
        a.setTarget1(u);
        a.setTarget2(p);
        Date d = new Date();
        a.setDate(d);
//        HistoryEvent his=new HistoryEvent();
//        his.setHistoryevent(a.toString());
//        em.persist(his);
        em.persist(a);
        return "success";
    }
}
